<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>DB_File - Perl5 access to Berkeley DB version 1.x - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/DB_File">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="DB_File.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/DB_File">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/DB_File">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/DB_File">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/DB_File">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/DB_File">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/DB_File">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/DB_File">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/DB_File">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/DB_File">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/DB_File">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/DB_File">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/DB_File">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/DB_File">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/DB_File">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/DB_File">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/DB_File">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/DB_File">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/DB_File">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/DB_File">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/DB_File">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/DB_File">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/DB_File">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/DB_File">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/DB_File">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/DB_File">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/DB_File">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/DB_File">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/DB_File">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/DB_File">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/DB_File">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/DB_File">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/DB_File">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/DB_File">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/DB_File">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/DB_File">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/DB_File">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/DB_File">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/DB_File">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/DB_File">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/DB_File">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/DB_File">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/DB_File">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/DB_File">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/DB_File">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/DB_File">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/DB_File">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/DB_File">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/DB_File">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/DB_File">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/DB_File">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/DB_File">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/DB_File">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/DB_File">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/DB_File">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/DB_File">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/DB_File">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/DB_File">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/DB_File">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/DB_File">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/DB_File">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/DB_File">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/DB_File">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/DB_File">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/DB_File">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/DB_File">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/DB_File">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/DB_File">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/DB_File">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/DB_File">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/DB_File">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/DB_File">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/DB_File">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/DB_File">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/DB_File">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/DB_File">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/DB_File">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/DB_File">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/DB_File">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/DB_File">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/DB_File">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="DB_File.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/DB_File">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/DB_File">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/DB_File">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/DB_File">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/DB_File">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/DB_File">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/DB_File">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/DB_File">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/DB_File">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/DB_File">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/DB_File">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/DB_File">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/DB_File">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/DB_File">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/DB_File">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/DB_File">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/DB_File">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/DB_File">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/DB_File">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/DB_File">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/DB_File">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/DB_File">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/DB_File">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/DB_File">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/DB_File">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/DB_File">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/DB_File">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/DB_File">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/DB_File">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/DB_File">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/DB_File">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/DB_File">5.35.1</a>
          <a class="dropdown-item active" href="DB_File.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="DB_File.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="DB_File.html">DB_File</a>
          <div id="more">
            (<a href="DB_File.txt">source</a>,
            <a href="https://metacpan.org/pod/DB_File">CPAN</a>)
          </div>
            <div id="moduleversion">version 1.855</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Using-DB_File-with-Berkeley-DB-version-2-or-greater">Using DB_File with Berkeley DB version 2 or greater</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Interface-to-Berkeley-DB">Interface to Berkeley DB</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Opening-a-Berkeley-DB-Database-File">Opening a Berkeley DB Database File</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Default-Parameters">Default Parameters</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#In-Memory-Databases">In Memory Databases</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#DB_HASH1">DB_HASH</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#A-Simple-Example">A Simple Example</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#DB_BTREE1">DB_BTREE</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Changing-the-BTREE-sort-order">Changing the BTREE sort order</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Handling-Duplicate-Keys">Handling Duplicate Keys</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#The-get_dup()-Method">The get_dup() Method</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#The-find_dup()-Method">The find_dup() Method</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#The-del_dup()-Method">The del_dup() Method</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Matching-Partial-Keys">Matching Partial Keys</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#DB_RECNO1">DB_RECNO</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#The-&#39;bval&#39;-Option">The &#39;bval&#39; Option</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#A-Simple-Example1">A Simple Example</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Extra-RECNO-Methods">Extra RECNO Methods</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Another-Example">Another Example</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#THE-API-INTERFACE">THE API INTERFACE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#DBM-FILTERS">DBM FILTERS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#DBM-Filter-Low-level-API">DBM Filter Low-level API</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#The-Filter">The Filter</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#An-Example-the-NULL-termination-problem.">An Example -- the NULL termination problem.</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Another-Example-Key-is-a-C-int.">Another Example -- Key is a C int.</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#HINTS-AND-TIPS">HINTS AND TIPS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Locking:-The-Trouble-with-fd">Locking: The Trouble with fd</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Safe-ways-to-lock-a-database">Safe ways to lock a database</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Sharing-Databases-With-C-Applications">Sharing Databases With C Applications</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#The-untie()-Gotcha">The untie() Gotcha</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#COMMON-QUESTIONS">COMMON QUESTIONS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="DB_File.html#Why-is-there-Perl-source-in-my-database?">Why is there Perl source in my database?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#How-do-I-store-complex-data-structures-with-DB_File?">How do I store complex data structures with DB_File?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#What-does-%22wide-character-in-subroutine-entry%22-mean?">What does &quot;wide character in subroutine entry&quot; mean?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#What-does-%22Invalid-Argument%22-mean?">What does &quot;Invalid Argument&quot; mean?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#What-does-%22Bareword-&#39;DB_File&#39;-not-allowed%22-mean?">What does &quot;Bareword &#39;DB_File&#39; not allowed&quot; mean?</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#REFERENCES">REFERENCES</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#HISTORY">HISTORY</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#BUGS">BUGS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#SUPPORT">SUPPORT</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#AVAILABILITY">AVAILABILITY</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#COPYRIGHT">COPYRIGHT</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#SEE-ALSO">SEE ALSO</a>
              </li>
              <li>
                <a class="text-decoration-none" href="DB_File.html#AUTHOR">AUTHOR</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="DB_File.html#NAME">#</a>NAME</h1>

<p>DB_File - Perl5 access to Berkeley DB version 1.x</p>

<h1 id="SYNOPSIS"><a class="permalink" href="DB_File.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<pre><code>use DB_File;

[$X =] tie %hash,  &#39;DB_File&#39;, [$filename, $flags, $mode, $DB_HASH] ;
[$X =] tie %hash,  &#39;DB_File&#39;, $filename, $flags, $mode, $DB_BTREE ;
[$X =] tie @array, &#39;DB_File&#39;, $filename, $flags, $mode, $DB_RECNO ;

$status = $X-&gt;del($key [, $flags]) ;
$status = $X-&gt;put($key, $value [, $flags]) ;
$status = $X-&gt;get($key, $value [, $flags]) ;
$status = $X-&gt;seq($key, $value, $flags) ;
$status = $X-&gt;sync([$flags]) ;
$status = $X-&gt;fd ;

# BTREE only
$count = $X-&gt;get_dup($key) ;
@list  = $X-&gt;get_dup($key) ;
%list  = $X-&gt;get_dup($key, 1) ;
$status = $X-&gt;find_dup($key, $value) ;
$status = $X-&gt;del_dup($key, $value) ;

# RECNO only
$a = $X-&gt;length;
$a = $X-&gt;pop ;
$X-&gt;push(list);
$a = $X-&gt;shift;
$X-&gt;unshift(list);
@r = $X-&gt;splice(offset, length, elements);

# DBM Filters
$old_filter = $db-&gt;filter_store_key  ( sub { ... } ) ;
$old_filter = $db-&gt;filter_store_value( sub { ... } ) ;
$old_filter = $db-&gt;filter_fetch_key  ( sub { ... } ) ;
$old_filter = $db-&gt;filter_fetch_value( sub { ... } ) ;

untie %hash ;
untie @array ;</code></pre>

<h1 id="DESCRIPTION"><a class="permalink" href="DB_File.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p><b>DB_File</b> is a module which allows Perl programs to make use of the facilities provided by Berkeley DB version 1.x (if you have a newer version of DB, see <a href="DB_File.html#Using-DB_File-with-Berkeley-DB-version-2-or-greater">&quot;Using DB_File with Berkeley DB version 2 or greater&quot;</a>). It is assumed that you have a copy of the Berkeley DB manual pages at hand when reading this documentation. The interface defined here mirrors the Berkeley DB interface closely.</p>

<p>Berkeley DB is a C library which provides a consistent interface to a number of database formats. <b>DB_File</b> provides an interface to all three of the database types currently supported by Berkeley DB.</p>

<p>The file types are:</p>

<dl>

<dt id="DB_HASH"><a class="permalink" href="DB_File.html#DB_HASH">#</a><b>DB_HASH</b></dt>
<dd>

<p>This database type allows arbitrary key/value pairs to be stored in data files. This is equivalent to the functionality provided by other hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though, the files created using DB_HASH are not compatible with any of the other packages mentioned.</p>

<p>A default hashing algorithm, which will be adequate for most applications, is built into Berkeley DB. If you do need to use your own hashing algorithm it is possible to write your own in Perl and have <b>DB_File</b> use it instead.</p>

</dd>
<dt id="DB_BTREE"><a class="permalink" href="DB_File.html#DB_BTREE">#</a><b>DB_BTREE</b></dt>
<dd>

<p>The btree format allows arbitrary key/value pairs to be stored in a sorted, balanced binary tree.</p>

<p>As with the DB_HASH format, it is possible to provide a user defined Perl routine to perform the comparison of keys. By default, though, the keys are stored in lexical order.</p>

</dd>
<dt id="DB_RECNO"><a class="permalink" href="DB_File.html#DB_RECNO">#</a><b>DB_RECNO</b></dt>
<dd>

<p>DB_RECNO allows both fixed-length and variable-length flat text files to be manipulated using the same key/value pair interface as in DB_HASH and DB_BTREE. In this case the key will consist of a record (line) number.</p>

</dd>
</dl>

<h2 id="Using-DB_File-with-Berkeley-DB-version-2-or-greater"><a class="permalink" href="DB_File.html#Using-DB_File-with-Berkeley-DB-version-2-or-greater">#</a><a id="Using"></a>Using DB_File with Berkeley DB version 2 or greater</h2>

<p>Although <b>DB_File</b> is intended to be used with Berkeley DB version 1, it can also be used with version 2, 3 or 4. In this case the interface is limited to the functionality provided by Berkeley DB 1.x. Anywhere the version 2 or greater interface differs, <b>DB_File</b> arranges for it to work like version 1. This feature allows <b>DB_File</b> scripts that were built with version 1 to be migrated to version 2 or greater without any changes.</p>

<p>If you want to make use of the new features available in Berkeley DB 2.x or greater, use the Perl module <a href="https://metacpan.org/pod/BerkeleyDB">BerkeleyDB</a> instead.</p>

<p><b>Note:</b> The database file format has changed multiple times in Berkeley DB version 2, 3 and 4. If you cannot recreate your databases, you must dump any existing databases with either the <code>db_dump</code> or the <code>db_dump185</code> utility that comes with Berkeley DB. Once you have rebuilt DB_File to use Berkeley DB version 2 or greater, your databases can be recreated using <code>db_load</code>. Refer to the Berkeley DB documentation for further details.</p>

<p>Please read <a href="DB_File.html#COPYRIGHT">&quot;COPYRIGHT&quot;</a> before using version 2.x or greater of Berkeley DB with DB_File.</p>

<h2 id="Interface-to-Berkeley-DB"><a class="permalink" href="DB_File.html#Interface-to-Berkeley-DB">#</a><a id="Interface"></a>Interface to Berkeley DB</h2>

<p><b>DB_File</b> allows access to Berkeley DB files using the tie() mechanism in Perl 5 (for full details, see <a href="perlfunc.html#tie%28%29">&quot;tie()&quot; in perlfunc</a>). This facility allows <b>DB_File</b> to access Berkeley DB files using either an associative array (for DB_HASH &amp; DB_BTREE file types) or an ordinary array (for the DB_RECNO file type).</p>

<p>In addition to the tie() interface, it is also possible to access most of the functions provided in the Berkeley DB API directly. See <a href="DB_File.html#THE-API-INTERFACE">&quot;THE API INTERFACE&quot;</a>.</p>

<h2 id="Opening-a-Berkeley-DB-Database-File"><a class="permalink" href="DB_File.html#Opening-a-Berkeley-DB-Database-File">#</a><a id="Opening"></a>Opening a Berkeley DB Database File</h2>

<p>Berkeley DB uses the function dbopen() to open or create a database. Here is the C prototype for dbopen():</p>

<pre><code class="plaintext">DB*
dbopen (const char * file, int flags, int mode,
        DBTYPE type, const void * openinfo)</code></pre>

<p>The parameter <code>type</code> is an enumeration which specifies which of the 3 interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used. Depending on which of these is actually chosen, the final parameter, <i>openinfo</i> points to a data structure which allows tailoring of the specific interface method.</p>

<p>This interface is handled slightly differently in <b>DB_File</b>. Here is an equivalent call using <b>DB_File</b>:</p>

<pre><code>tie %array, &#39;DB_File&#39;, $filename, $flags, $mode, $DB_HASH ;</code></pre>

<p>The <code>filename</code>, <code>flags</code> and <code>mode</code> parameters are the direct equivalent of their dbopen() counterparts. The final parameter $DB_HASH performs the function of both the <code>type</code> and <code>openinfo</code> parameters in dbopen().</p>

<p>In the example above $DB_HASH is actually a pre-defined reference to a hash object. <b>DB_File</b> has three of these pre-defined references. Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO.</p>

<p>The keys allowed in each of these pre-defined references is limited to the names used in the equivalent C structure. So, for example, the $DB_HASH reference will only allow keys called <code>bsize</code>, <code>cachesize</code>, <code>ffactor</code>, <code>hash</code>, <code>lorder</code> and <code>nelem</code>.</p>

<p>To change one of these elements, just assign to it like this:</p>

<pre><code>$DB_HASH-&gt;{&#39;cachesize&#39;} = 10000 ;</code></pre>

<p>The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are usually adequate for most applications. If you do need to create extra instances of these objects, constructors are available for each file type.</p>

<p>Here are examples of the constructors and the valid options available for DB_HASH, DB_BTREE and DB_RECNO respectively.</p>

<pre><code>$a = DB_File::HASHINFO-&gt;new();
$a-&gt;{&#39;bsize&#39;} ;
$a-&gt;{&#39;cachesize&#39;} ;
$a-&gt;{&#39;ffactor&#39;};
$a-&gt;{&#39;hash&#39;} ;
$a-&gt;{&#39;lorder&#39;} ;
$a-&gt;{&#39;nelem&#39;} ;

$b = DB_File::BTREEINFO-&gt;new();
$b-&gt;{&#39;flags&#39;} ;
$b-&gt;{&#39;cachesize&#39;} ;
$b-&gt;{&#39;maxkeypage&#39;} ;
$b-&gt;{&#39;minkeypage&#39;} ;
$b-&gt;{&#39;psize&#39;} ;
$b-&gt;{&#39;compare&#39;} ;
$b-&gt;{&#39;prefix&#39;} ;
$b-&gt;{&#39;lorder&#39;} ;

$c = DB_File::RECNOINFO-&gt;new();
$c-&gt;{&#39;bval&#39;} ;
$c-&gt;{&#39;cachesize&#39;} ;
$c-&gt;{&#39;psize&#39;} ;
$c-&gt;{&#39;flags&#39;} ;
$c-&gt;{&#39;lorder&#39;} ;
$c-&gt;{&#39;reclen&#39;} ;
$c-&gt;{&#39;bfname&#39;} ;</code></pre>

<p>The values stored in the hashes above are mostly the direct equivalent of their C counterpart. Like their C counterparts, all are set to a default values - that means you don&#39;t have to set <i>all</i> of the values when you only want to change one. Here is an example:</p>

<pre><code>$a = DB_File::HASHINFO-&gt;new();
$a-&gt;{&#39;cachesize&#39;} =  12345 ;
tie %y, &#39;DB_File&#39;, &quot;filename&quot;, $flags, 0777, $a ;</code></pre>

<p>A few of the options need extra discussion here. When used, the C equivalent of the keys <code>hash</code>, <code>compare</code> and <code>prefix</code> store pointers to C functions. In <b>DB_File</b> these keys are used to store references to Perl subs. Below are templates for each of the subs:</p>

<pre><code>sub hash
{
    my ($data) = @_ ;
    ...
    # return the hash value for $data
    return $hash ;
}

sub compare
{
    my ($key, $key2) = @_ ;
    ...
    # return  0 if $key1 eq $key2
    #        -1 if $key1 lt $key2
    #         1 if $key1 gt $key2
    return (-1 , 0 or 1) ;
}

sub prefix
{
    my ($key, $key2) = @_ ;
    ...
    # return number of bytes of $key2 which are
    # necessary to determine that it is greater than $key1
    return $bytes ;
}</code></pre>

<p>See <a href="DB_File.html#Changing-the-BTREE-sort-order">&quot;Changing the BTREE sort order&quot;</a> for an example of using the <code>compare</code> template.</p>

<p>If you are using the DB_RECNO interface and you intend making use of <code>bval</code>, you should check out <a href="DB_File.html#The-%27bval%27-Option">&quot;The &#39;bval&#39; Option&quot;</a>.</p>

<h2 id="Default-Parameters"><a class="permalink" href="DB_File.html#Default-Parameters">#</a><a id="Default"></a>Default Parameters</h2>

<p>It is possible to omit some or all of the final 4 parameters in the call to <code>tie</code> and let them take default values. As DB_HASH is the most common file format used, the call:</p>

<pre><code>tie %A, &quot;DB_File&quot;, &quot;filename&quot; ;</code></pre>

<p>is equivalent to:</p>

<pre><code>tie %A, &quot;DB_File&quot;, &quot;filename&quot;, O_CREAT|O_RDWR, 0666, $DB_HASH ;</code></pre>

<p>It is also possible to omit the filename parameter as well, so the call:</p>

<pre><code>tie %A, &quot;DB_File&quot; ;</code></pre>

<p>is equivalent to:</p>

<pre><code>tie %A, &quot;DB_File&quot;, undef, O_CREAT|O_RDWR, 0666, $DB_HASH ;</code></pre>

<p>See <a href="DB_File.html#In-Memory-Databases">&quot;In Memory Databases&quot;</a> for a discussion on the use of <code>undef</code> in place of a filename.</p>

<h2 id="In-Memory-Databases"><a class="permalink" href="DB_File.html#In-Memory-Databases">#</a><a id="In"></a>In Memory Databases</h2>

<p>Berkeley DB allows the creation of in-memory databases by using NULL (that is, a <code>(char *)0</code> in C) in place of the filename. <b>DB_File</b> uses <code>undef</code> instead of NULL to provide this functionality.</p>

<h1 id="DB_HASH1"><a class="permalink" href="DB_File.html#DB_HASH1">#</a>DB_HASH</h1>

<p>The DB_HASH file format is probably the most commonly used of the three file formats that <b>DB_File</b> supports. It is also very straightforward to use.</p>

<h2 id="A-Simple-Example"><a class="permalink" href="DB_File.html#A-Simple-Example">#</a><a id="A"></a>A Simple Example</h2>

<p>This example shows how to create a database, add key/value pairs to the database, delete keys/value pairs and finally how to enumerate the contents of the database.</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;
our (%h, $k, $v) ;

unlink &quot;fruit&quot; ;
tie %h, &quot;DB_File&quot;, &quot;fruit&quot;, O_RDWR|O_CREAT, 0666, $DB_HASH
    or die &quot;Cannot open file &#39;fruit&#39;: $!\n&quot;;

# Add a few key/value pairs to the file
$h{&quot;apple&quot;} = &quot;red&quot; ;
$h{&quot;orange&quot;} = &quot;orange&quot; ;
$h{&quot;banana&quot;} = &quot;yellow&quot; ;
$h{&quot;tomato&quot;} = &quot;red&quot; ;

# Check for existence of a key
print &quot;Banana Exists\n\n&quot; if $h{&quot;banana&quot;} ;

# Delete a key/value pair.
delete $h{&quot;apple&quot;} ;

# print the contents of the file
while (($k, $v) = each %h)
  { print &quot;$k -&gt; $v\n&quot; }

untie %h ;</code></pre>

<p>here is the output:</p>

<pre><code class="plaintext">Banana Exists

orange -&gt; orange
tomato -&gt; red
banana -&gt; yellow</code></pre>

<p>Note that the like ordinary associative arrays, the order of the keys retrieved is in an apparently random order.</p>

<h1 id="DB_BTREE1"><a class="permalink" href="DB_File.html#DB_BTREE1">#</a>DB_BTREE</h1>

<p>The DB_BTREE format is useful when you want to store data in a given order. By default the keys will be stored in lexical order, but as you will see from the example shown in the next section, it is very easy to define your own sorting function.</p>

<h2 id="Changing-the-BTREE-sort-order"><a class="permalink" href="DB_File.html#Changing-the-BTREE-sort-order">#</a><a id="Changing"></a>Changing the BTREE sort order</h2>

<p>This script shows how to override the default sorting algorithm that BTREE uses. Instead of using the normal lexical ordering, a case insensitive compare function will be used.</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my %h ;

sub Compare
{
    my ($key1, $key2) = @_ ;
    &quot;\L$key1&quot; cmp &quot;\L$key2&quot; ;
}

# specify the Perl sub that will do the comparison
$DB_BTREE-&gt;{&#39;compare&#39;} = \&amp;Compare ;

unlink &quot;tree&quot; ;
tie %h, &quot;DB_File&quot;, &quot;tree&quot;, O_RDWR|O_CREAT, 0666, $DB_BTREE
    or die &quot;Cannot open file &#39;tree&#39;: $!\n&quot; ;

# Add a key/value pair to the file
$h{&#39;Wall&#39;} = &#39;Larry&#39; ;
$h{&#39;Smith&#39;} = &#39;John&#39; ;
$h{&#39;mouse&#39;} = &#39;mickey&#39; ;
$h{&#39;duck&#39;}  = &#39;donald&#39; ;

# Delete
delete $h{&quot;duck&quot;} ;

# Cycle through the keys printing them in order.
# Note it is not necessary to sort the keys as
# the btree will have kept them in order automatically.
foreach (keys %h)
  { print &quot;$_\n&quot; }

untie %h ;</code></pre>

<p>Here is the output from the code above.</p>

<pre><code class="plaintext">mouse
Smith
Wall</code></pre>

<p>There are a few point to bear in mind if you want to change the ordering in a BTREE database:</p>

<ol>

<li><p>The new compare function must be specified when you create the database.</p>

</li>
<li><p>You cannot change the ordering once the database has been created. Thus you must use the same compare function every time you access the database.</p>

</li>
<li><p>Duplicate keys are entirely defined by the comparison function. In the case-insensitive example above, the keys: &#39;KEY&#39; and &#39;key&#39; would be considered duplicates, and assigning to the second one would overwrite the first. If duplicates are allowed for (with the R_DUP flag discussed below), only a single copy of duplicate keys is stored in the database --- so (again with example above) assigning three values to the keys: &#39;KEY&#39;, &#39;Key&#39;, and &#39;key&#39; would leave just the first key: &#39;KEY&#39; in the database with three values. For some situations this results in information loss, so care should be taken to provide fully qualified comparison functions when necessary. For example, the above comparison routine could be modified to additionally compare case-sensitively if two keys are equal in the case insensitive comparison:</p>

<pre><code>sub compare {
    my($key1, $key2) = @_;
    lc $key1 cmp lc $key2 ||
    $key1 cmp $key2;
}</code></pre>

<p>And now you will only have duplicates when the keys themselves are truly the same. (note: in versions of the db library prior to about November 1996, such duplicate keys were retained so it was possible to recover the original keys in sets of keys that compared as equal).</p>

</li>
</ol>

<h2 id="Handling-Duplicate-Keys"><a class="permalink" href="DB_File.html#Handling-Duplicate-Keys">#</a><a id="Handling"></a>Handling Duplicate Keys</h2>

<p>The BTREE file type optionally allows a single key to be associated with an arbitrary number of values. This option is enabled by setting the flags element of <code>$DB_BTREE</code> to R_DUP when creating the database.</p>

<p>There are some difficulties in using the tied hash interface if you want to manipulate a BTREE database with duplicate keys. Consider this code:</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my ($filename, %h) ;

$filename = &quot;tree&quot; ;
unlink $filename ;

# Enable duplicate records
$DB_BTREE-&gt;{&#39;flags&#39;} = R_DUP ;

tie %h, &quot;DB_File&quot;, $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
    or die &quot;Cannot open $filename: $!\n&quot;;

# Add some key/value pairs to the file
$h{&#39;Wall&#39;} = &#39;Larry&#39; ;
$h{&#39;Wall&#39;} = &#39;Brick&#39; ; # Note the duplicate key
$h{&#39;Wall&#39;} = &#39;Brick&#39; ; # Note the duplicate key and value
$h{&#39;Smith&#39;} = &#39;John&#39; ;
$h{&#39;mouse&#39;} = &#39;mickey&#39; ;

# iterate through the associative array
# and print each key/value pair.
foreach (sort keys %h)
  { print &quot;$_  -&gt; $h{$_}\n&quot; }

untie %h ;</code></pre>

<p>Here is the output:</p>

<pre><code class="plaintext">Smith   -&gt; John
Wall    -&gt; Larry
Wall    -&gt; Larry
Wall    -&gt; Larry
mouse   -&gt; mickey</code></pre>

<p>As you can see 3 records have been successfully created with key <code>Wall</code> - the only thing is, when they are retrieved from the database they <i>seem</i> to have the same value, namely <code>Larry</code>. The problem is caused by the way that the associative array interface works. Basically, when the associative array interface is used to fetch the value associated with a given key, it will only ever retrieve the first value.</p>

<p>Although it may not be immediately obvious from the code above, the associative array interface can be used to write values with duplicate keys, but it cannot be used to read them back from the database.</p>

<p>The way to get around this problem is to use the Berkeley DB API method called <code>seq</code>. This method allows sequential access to key/value pairs. See <a href="DB_File.html#THE-API-INTERFACE">&quot;THE API INTERFACE&quot;</a> for details of both the <code>seq</code> method and the API in general.</p>

<p>Here is the script above rewritten using the <code>seq</code> API method.</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my ($filename, $x, %h, $status, $key, $value) ;

$filename = &quot;tree&quot; ;
unlink $filename ;

# Enable duplicate records
$DB_BTREE-&gt;{&#39;flags&#39;} = R_DUP ;

$x = tie %h, &quot;DB_File&quot;, $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
    or die &quot;Cannot open $filename: $!\n&quot;;

# Add some key/value pairs to the file
$h{&#39;Wall&#39;} = &#39;Larry&#39; ;
$h{&#39;Wall&#39;} = &#39;Brick&#39; ; # Note the duplicate key
$h{&#39;Wall&#39;} = &#39;Brick&#39; ; # Note the duplicate key and value
$h{&#39;Smith&#39;} = &#39;John&#39; ;
$h{&#39;mouse&#39;} = &#39;mickey&#39; ;

# iterate through the btree using seq
# and print each key/value pair.
$key = $value = 0 ;
for ($status = $x-&gt;seq($key, $value, R_FIRST) ;
     $status == 0 ;
     $status = $x-&gt;seq($key, $value, R_NEXT) )
  {  print &quot;$key -&gt; $value\n&quot; }

undef $x ;
untie %h ;</code></pre>

<p>that prints:</p>

<pre><code class="plaintext">Smith   -&gt; John
Wall    -&gt; Brick
Wall    -&gt; Brick
Wall    -&gt; Larry
mouse   -&gt; mickey</code></pre>

<p>This time we have got all the key/value pairs, including the multiple values associated with the key <code>Wall</code>.</p>

<p>To make life easier when dealing with duplicate keys, <b>DB_File</b> comes with a few utility methods.</p>

<h2 id="The-get_dup()-Method"><a class="permalink" href="DB_File.html#The-get_dup()-Method">#</a><a id="The"></a><a id="The-get_dup-Method"></a>The get_dup() Method</h2>

<p>The <code>get_dup</code> method assists in reading duplicate values from BTREE databases. The method can take the following forms:</p>

<pre><code>$count = $x-&gt;get_dup($key) ;
@list  = $x-&gt;get_dup($key) ;
%list  = $x-&gt;get_dup($key, 1) ;</code></pre>

<p>In a scalar context the method returns the number of values associated with the key, <code>$key</code>.</p>

<p>In list context, it returns all the values which match <code>$key</code>. Note that the values will be returned in an apparently random order.</p>

<p>In list context, if the second parameter is present and evaluates TRUE, the method returns an associative array. The keys of the associative array correspond to the values that matched in the BTREE and the values of the array are a count of the number of times that particular value occurred in the BTREE.</p>

<p>So assuming the database created above, we can use <code>get_dup</code> like this:</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my ($filename, $x, %h) ;

$filename = &quot;tree&quot; ;

# Enable duplicate records
$DB_BTREE-&gt;{&#39;flags&#39;} = R_DUP ;

$x = tie %h, &quot;DB_File&quot;, $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
    or die &quot;Cannot open $filename: $!\n&quot;;

my $cnt  = $x-&gt;get_dup(&quot;Wall&quot;) ;
print &quot;Wall occurred $cnt times\n&quot; ;

my %hash = $x-&gt;get_dup(&quot;Wall&quot;, 1) ;
print &quot;Larry is there\n&quot; if $hash{&#39;Larry&#39;} ;
print &quot;There are $hash{&#39;Brick&#39;} Brick Walls\n&quot; ;

my @list = sort $x-&gt;get_dup(&quot;Wall&quot;) ;
print &quot;Wall =&gt;      [@list]\n&quot; ;

@list = $x-&gt;get_dup(&quot;Smith&quot;) ;
print &quot;Smith =&gt;     [@list]\n&quot; ;

@list = $x-&gt;get_dup(&quot;Dog&quot;) ;
print &quot;Dog =&gt;       [@list]\n&quot; ;</code></pre>

<p>and it will print:</p>

<pre><code class="plaintext">Wall occurred 3 times
Larry is there
There are 2 Brick Walls
Wall =&gt;     [Brick Brick Larry]
Smith =&gt;    [John]
Dog =&gt;      []</code></pre>

<h2 id="The-find_dup()-Method"><a class="permalink" href="DB_File.html#The-find_dup()-Method">#</a><a id="The1"></a><a id="The-find_dup-Method"></a>The find_dup() Method</h2>

<pre><code>$status = $X-&gt;find_dup($key, $value) ;</code></pre>

<p>This method checks for the existence of a specific key/value pair. If the pair exists, the cursor is left pointing to the pair and the method returns 0. Otherwise the method returns a non-zero value.</p>

<p>Assuming the database from the previous example:</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my ($filename, $x, %h, $found) ;

$filename = &quot;tree&quot; ;

# Enable duplicate records
$DB_BTREE-&gt;{&#39;flags&#39;} = R_DUP ;

$x = tie %h, &quot;DB_File&quot;, $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
    or die &quot;Cannot open $filename: $!\n&quot;;

$found = ( $x-&gt;find_dup(&quot;Wall&quot;, &quot;Larry&quot;) == 0 ? &quot;&quot; : &quot;not&quot;) ;
print &quot;Larry Wall is $found there\n&quot; ;

$found = ( $x-&gt;find_dup(&quot;Wall&quot;, &quot;Harry&quot;) == 0 ? &quot;&quot; : &quot;not&quot;) ;
print &quot;Harry Wall is $found there\n&quot; ;

undef $x ;
untie %h ;</code></pre>

<p>prints this</p>

<pre><code class="plaintext">Larry Wall is  there
Harry Wall is not there</code></pre>

<h2 id="The-del_dup()-Method"><a class="permalink" href="DB_File.html#The-del_dup()-Method">#</a><a id="The2"></a><a id="The-del_dup-Method"></a>The del_dup() Method</h2>

<pre><code>$status = $X-&gt;del_dup($key, $value) ;</code></pre>

<p>This method deletes a specific key/value pair. It returns 0 if they exist and have been deleted successfully. Otherwise the method returns a non-zero value.</p>

<p>Again assuming the existence of the <code>tree</code> database</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my ($filename, $x, %h, $found) ;

$filename = &quot;tree&quot; ;

# Enable duplicate records
$DB_BTREE-&gt;{&#39;flags&#39;} = R_DUP ;

$x = tie %h, &quot;DB_File&quot;, $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
    or die &quot;Cannot open $filename: $!\n&quot;;

$x-&gt;del_dup(&quot;Wall&quot;, &quot;Larry&quot;) ;

$found = ( $x-&gt;find_dup(&quot;Wall&quot;, &quot;Larry&quot;) == 0 ? &quot;&quot; : &quot;not&quot;) ;
print &quot;Larry Wall is $found there\n&quot; ;

undef $x ;
untie %h ;</code></pre>

<p>prints this</p>

<pre><code class="plaintext">Larry Wall is not there</code></pre>

<h2 id="Matching-Partial-Keys"><a class="permalink" href="DB_File.html#Matching-Partial-Keys">#</a><a id="Matching"></a>Matching Partial Keys</h2>

<p>The BTREE interface has a feature which allows partial keys to be matched. This functionality is <i>only</i> available when the <code>seq</code> method is used along with the R_CURSOR flag.</p>

<pre><code>$x-&gt;seq($key, $value, R_CURSOR) ;</code></pre>

<p>Here is the relevant quote from the dbopen man page where it defines the use of the R_CURSOR flag with seq:</p>

<pre><code class="plaintext">Note, for the DB_BTREE access method, the returned key is not
necessarily an exact match for the specified key. The returned key
is the smallest key greater than or equal to the specified key,
permitting partial key matches and range searches.</code></pre>

<p>In the example script below, the <code>match</code> sub uses this feature to find and print the first matching key/value pair given a partial key.</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;
use Fcntl ;

my ($filename, $x, %h, $st, $key, $value) ;

sub match
{
    my $key = shift ;
    my $value = 0;
    my $orig_key = $key ;
    $x-&gt;seq($key, $value, R_CURSOR) ;
    print &quot;$orig_key\t-&gt; $key\t-&gt; $value\n&quot; ;
}

$filename = &quot;tree&quot; ;
unlink $filename ;

$x = tie %h, &quot;DB_File&quot;, $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
    or die &quot;Cannot open $filename: $!\n&quot;;

# Add some key/value pairs to the file
$h{&#39;mouse&#39;} = &#39;mickey&#39; ;
$h{&#39;Wall&#39;} = &#39;Larry&#39; ;
$h{&#39;Walls&#39;} = &#39;Brick&#39; ;
$h{&#39;Smith&#39;} = &#39;John&#39; ;


$key = $value = 0 ;
print &quot;IN ORDER\n&quot; ;
for ($st = $x-&gt;seq($key, $value, R_FIRST) ;
     $st == 0 ;
     $st = $x-&gt;seq($key, $value, R_NEXT) )

  {  print &quot;$key    -&gt; $value\n&quot; }

print &quot;\nPARTIAL MATCH\n&quot; ;

match &quot;Wa&quot; ;
match &quot;A&quot; ;
match &quot;a&quot; ;

undef $x ;
untie %h ;</code></pre>

<p>Here is the output:</p>

<pre><code class="plaintext">IN ORDER
Smith -&gt; John
Wall  -&gt; Larry
Walls -&gt; Brick
mouse -&gt; mickey

PARTIAL MATCH
Wa -&gt; Wall  -&gt; Larry
A  -&gt; Smith -&gt; John
a  -&gt; mouse -&gt; mickey</code></pre>

<h1 id="DB_RECNO1"><a class="permalink" href="DB_File.html#DB_RECNO1">#</a>DB_RECNO</h1>

<p>DB_RECNO provides an interface to flat text files. Both variable and fixed length records are supported.</p>

<p>In order to make RECNO more compatible with Perl, the array offset for all RECNO arrays begins at 0 rather than 1 as in Berkeley DB.</p>

<p>As with normal Perl arrays, a RECNO array can be accessed using negative indexes. The index -1 refers to the last element of the array, -2 the second last, and so on. Attempting to access an element before the start of the array will raise a fatal run-time error.</p>

<h2 id="The-&#39;bval&#39;-Option"><a class="permalink" href="DB_File.html#The-&#39;bval&#39;-Option">#</a><a id="The3"></a><a id="The-bval-Option"></a>The &#39;bval&#39; Option</h2>

<p>The operation of the bval option warrants some discussion. Here is the definition of bval from the Berkeley DB 1.85 recno manual page:</p>

<pre><code class="plaintext">The delimiting byte to be used to mark  the  end  of  a
record for variable-length records, and the pad charac-
ter for fixed-length records.  If no  value  is  speci-
fied,  newlines  (``\n&#39;&#39;)  are  used to mark the end of
variable-length records and  fixed-length  records  are
padded with spaces.</code></pre>

<p>The second sentence is wrong. In actual fact bval will only default to <code>&quot;\n&quot;</code> when the openinfo parameter in dbopen is NULL. If a non-NULL openinfo parameter is used at all, the value that happens to be in bval will be used. That means you always have to specify bval when making use of any of the options in the openinfo parameter. This documentation error will be fixed in the next release of Berkeley DB.</p>

<p>That clarifies the situation with regards Berkeley DB itself. What about <b>DB_File</b>? Well, the behavior defined in the quote above is quite useful, so <b>DB_File</b> conforms to it.</p>

<p>That means that you can specify other options (e.g. cachesize) and still have bval default to <code>&quot;\n&quot;</code> for variable length records, and space for fixed length records.</p>

<p>Also note that the bval option only allows you to specify a single byte as a delimiter.</p>

<h2 id="A-Simple-Example1"><a class="permalink" href="DB_File.html#A-Simple-Example1">#</a><a id="A1"></a>A Simple Example</h2>

<p>Here is a simple example that uses RECNO (if you are using a version of Perl earlier than 5.004_57 this example won&#39;t work -- see <a href="DB_File.html#Extra-RECNO-Methods">&quot;Extra RECNO Methods&quot;</a> for a workaround).</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my $filename = &quot;text&quot; ;
unlink $filename ;

my @h ;
tie @h, &quot;DB_File&quot;, $filename, O_RDWR|O_CREAT, 0666, $DB_RECNO
    or die &quot;Cannot open file &#39;text&#39;: $!\n&quot; ;

# Add a few key/value pairs to the file
$h[0] = &quot;orange&quot; ;
$h[1] = &quot;blue&quot; ;
$h[2] = &quot;yellow&quot; ;

push @h, &quot;green&quot;, &quot;black&quot; ;

my $elements = scalar @h ;
print &quot;The array contains $elements entries\n&quot; ;

my $last = pop @h ;
print &quot;popped $last\n&quot; ;

unshift @h, &quot;white&quot; ;
my $first = shift @h ;
print &quot;shifted $first\n&quot; ;

# Check for existence of a key
print &quot;Element 1 Exists with value $h[1]\n&quot; if $h[1] ;

# use a negative index
print &quot;The last element is $h[-1]\n&quot; ;
print &quot;The 2nd last element is $h[-2]\n&quot; ;

untie @h ;</code></pre>

<p>Here is the output from the script:</p>

<pre><code class="plaintext">The array contains 5 entries
popped black
shifted white
Element 1 Exists with value blue
The last element is green
The 2nd last element is yellow</code></pre>

<h2 id="Extra-RECNO-Methods"><a class="permalink" href="DB_File.html#Extra-RECNO-Methods">#</a><a id="Extra"></a>Extra RECNO Methods</h2>

<p>If you are using a version of Perl earlier than 5.004_57, the tied array interface is quite limited. In the example script above <code>push</code>, <code>pop</code>, <code>shift</code>, <code>unshift</code> or determining the array length will not work with a tied array.</p>

<p>To make the interface more useful for older versions of Perl, a number of methods are supplied with <b>DB_File</b> to simulate the missing array operations. All these methods are accessed via the object returned from the tie call.</p>

<p>Here are the methods:</p>

<dl>

<dt id="$X-&gt;push(list)-;"><a class="permalink" href="DB_File.html#$X-%3Epush(list)-;">#</a><a id="push"></a><a id="X-push-list"></a><b>$X-&gt;push(list) ;</b></dt>
<dd>

<p>Pushes the elements of <code>list</code> to the end of the array.</p>

</dd>
<dt id="$value-=-$X-&gt;pop-;"><a class="permalink" href="DB_File.html#$value-=-$X-%3Epop-;">#</a><a id="value-X-pop"></a><b>$value = $X-&gt;pop ;</b></dt>
<dd>

<p>Removes and returns the last element of the array.</p>

</dd>
<dt id="$X-&gt;shift"><a class="permalink" href="DB_File.html#$X-%3Eshift">#</a><a id="shift"></a><a id="X-shift"></a><b>$X-&gt;shift</b></dt>
<dd>

<p>Removes and returns the first element of the array.</p>

</dd>
<dt id="$X-&gt;unshift(list)-;"><a class="permalink" href="DB_File.html#$X-%3Eunshift(list)-;">#</a><a id="unshift"></a><a id="X-unshift-list"></a><b>$X-&gt;unshift(list) ;</b></dt>
<dd>

<p>Pushes the elements of <code>list</code> to the start of the array.</p>

</dd>
<dt id="$X-&gt;length"><a class="permalink" href="DB_File.html#$X-%3Elength">#</a><a id="length"></a><a id="X-length"></a><b>$X-&gt;length</b></dt>
<dd>

<p>Returns the number of elements in the array.</p>

</dd>
<dt id="$X-&gt;splice(offset,-length,-elements);"><a class="permalink" href="DB_File.html#$X-%3Esplice(offset,-length,-elements);">#</a><a id="splice"></a><a id="X-splice-offset-length-elements"></a><b>$X-&gt;splice(offset, length, elements);</b></dt>
<dd>

<p>Returns a splice of the array.</p>

</dd>
</dl>

<h2 id="Another-Example"><a class="permalink" href="DB_File.html#Another-Example">#</a><a id="Another"></a>Another Example</h2>

<p>Here is a more complete example that makes use of some of the methods described above. It also makes use of the API interface directly (see <a href="DB_File.html#THE-API-INTERFACE">&quot;THE API INTERFACE&quot;</a>).</p>

<pre><code>use warnings ;
use strict ;
my (@h, $H, $file, $i) ;
use DB_File ;
use Fcntl ;

$file = &quot;text&quot; ;

unlink $file ;

$H = tie @h, &quot;DB_File&quot;, $file, O_RDWR|O_CREAT, 0666, $DB_RECNO
    or die &quot;Cannot open file $file: $!\n&quot; ;

# first create a text file to play with
$h[0] = &quot;zero&quot; ;
$h[1] = &quot;one&quot; ;
$h[2] = &quot;two&quot; ;
$h[3] = &quot;three&quot; ;
$h[4] = &quot;four&quot; ;


# Print the records in order.
#
# The length method is needed here because evaluating a tied
# array in a scalar context does not return the number of
# elements in the array.

print &quot;\nORIGINAL\n&quot; ;
foreach $i (0 .. $H-&gt;length - 1) {
    print &quot;$i: $h[$i]\n&quot; ;
}

# use the push &amp; pop methods
$a = $H-&gt;pop ;
$H-&gt;push(&quot;last&quot;) ;
print &quot;\nThe last record was [$a]\n&quot; ;

# and the shift &amp; unshift methods
$a = $H-&gt;shift ;
$H-&gt;unshift(&quot;first&quot;) ;
print &quot;The first record was [$a]\n&quot; ;

# Use the API to add a new record after record 2.
$i = 2 ;
$H-&gt;put($i, &quot;Newbie&quot;, R_IAFTER) ;

# and a new record before record 1.
$i = 1 ;
$H-&gt;put($i, &quot;New One&quot;, R_IBEFORE) ;

# delete record 3
$H-&gt;del(3) ;

# now print the records in reverse order
print &quot;\nREVERSE\n&quot; ;
for ($i = $H-&gt;length - 1 ; $i &gt;= 0 ; -- $i)
  { print &quot;$i: $h[$i]\n&quot; }

# same again, but use the API functions instead
print &quot;\nREVERSE again\n&quot; ;
my ($s, $k, $v)  = (0, 0, 0) ;
for ($s = $H-&gt;seq($k, $v, R_LAST) ;
         $s == 0 ;
         $s = $H-&gt;seq($k, $v, R_PREV))
  { print &quot;$k: $v\n&quot; }

undef $H ;
untie @h ;</code></pre>

<p>and this is what it outputs:</p>

<pre><code class="plaintext">ORIGINAL
0: zero
1: one
2: two
3: three
4: four

The last record was [four]
The first record was [zero]

REVERSE
5: last
4: three
3: Newbie
2: one
1: New One
0: first

REVERSE again
5: last
4: three
3: Newbie
2: one
1: New One
0: first</code></pre>

<p>Notes:</p>

<ol>

<li><p>Rather than iterating through the array, <code>@h</code> like this:</p>

<pre><code>foreach $i (@h)</code></pre>

<p>it is necessary to use either this:</p>

<pre><code>foreach $i (0 .. $H-&gt;length - 1)</code></pre>

<p>or this:</p>

<pre><code>for ($a = $H-&gt;get($k, $v, R_FIRST) ;
     $a == 0 ;
     $a = $H-&gt;get($k, $v, R_NEXT) )</code></pre>

</li>
<li><p>Notice that both times the <code>put</code> method was used the record index was specified using a variable, <code>$i</code>, rather than the literal value itself. This is because <code>put</code> will return the record number of the inserted line via that parameter.</p>

</li>
</ol>

<h1 id="THE-API-INTERFACE"><a class="permalink" href="DB_File.html#THE-API-INTERFACE">#</a><a id="THE"></a>THE API INTERFACE</h1>

<p>As well as accessing Berkeley DB using a tied hash or array, it is also possible to make direct use of most of the API functions defined in the Berkeley DB documentation.</p>

<p>To do this you need to store a copy of the object returned from the tie.</p>

<pre><code>$db = tie %hash, &quot;DB_File&quot;, &quot;filename&quot; ;</code></pre>

<p>Once you have done that, you can access the Berkeley DB API functions as <b>DB_File</b> methods directly like this:</p>

<pre><code>$db-&gt;put($key, $value, R_NOOVERWRITE) ;</code></pre>

<p><b>Important:</b> If you have saved a copy of the object returned from <code>tie</code>, the underlying database file will <i>not</i> be closed until both the tied variable is untied and all copies of the saved object are destroyed.</p>

<pre><code>use DB_File ;
$db = tie %hash, &quot;DB_File&quot;, &quot;filename&quot;
    or die &quot;Cannot tie filename: $!&quot; ;
...
undef $db ;
untie %hash ;</code></pre>

<p>See <a href="DB_File.html#The-untie%28%29-Gotcha">&quot;The untie() Gotcha&quot;</a> for more details.</p>

<p>All the functions defined in <a href="https://perldoc.perl.org/5.35.0/dbopen">dbopen</a> are available except for close() and dbopen() itself. The <b>DB_File</b> method interface to the supported functions have been implemented to mirror the way Berkeley DB works whenever possible. In particular note that:</p>

<ul>

<li><p>The methods return a status value. All return 0 on success. All return -1 to signify an error and set <code>$!</code> to the exact error code. The return code 1 generally (but not always) means that the key specified did not exist in the database.</p>

<p>Other return codes are defined. See below and in the Berkeley DB documentation for details. The Berkeley DB documentation should be used as the definitive source.</p>

</li>
<li><p>Whenever a Berkeley DB function returns data via one of its parameters, the equivalent <b>DB_File</b> method does exactly the same.</p>

</li>
<li><p>If you are careful, it is possible to mix API calls with the tied hash/array interface in the same piece of code. Although only a few of the methods used to implement the tied interface currently make use of the cursor, you should always assume that the cursor has been changed any time the tied hash/array interface is used. As an example, this code will probably not do what you expect:</p>

<pre><code>$X = tie %x, &#39;DB_File&#39;, $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
    or die &quot;Cannot tie $filename: $!&quot; ;

# Get the first key/value pair and set  the cursor
$X-&gt;seq($key, $value, R_FIRST) ;

# this line will modify the cursor
$count = scalar keys %x ;

# Get the second key/value pair.
# oops, it didn&#39;t, it got the last key/value pair!
$X-&gt;seq($key, $value, R_NEXT) ;</code></pre>

<p>The code above can be rearranged to get around the problem, like this:</p>

<pre><code>$X = tie %x, &#39;DB_File&#39;, $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
    or die &quot;Cannot tie $filename: $!&quot; ;

# this line will modify the cursor
$count = scalar keys %x ;

# Get the first key/value pair and set  the cursor
$X-&gt;seq($key, $value, R_FIRST) ;

# Get the second key/value pair.
# worked this time.
$X-&gt;seq($key, $value, R_NEXT) ;</code></pre>

</li>
</ul>

<p>All the constants defined in <a href="https://perldoc.perl.org/5.35.0/dbopen">dbopen</a> for use in the flags parameters in the methods defined below are also available. Refer to the Berkeley DB documentation for the precise meaning of the flags values.</p>

<p>Below is a list of the methods available.</p>

<dl>

<dt id="$status-=-$X-&gt;get($key,-$value-[,-$flags])-;"><a class="permalink" href="DB_File.html#$status-=-$X-%3Eget($key,-$value-%5B,-$flags%5D)-;">#</a><a id="status-X-get-key-value-flags"></a><b>$status = $X-&gt;get($key, $value [, $flags]) ;</b></dt>
<dd>

<p>Given a key (<code>$key</code>) this method reads the value associated with it from the database. The value read from the database is returned in the <code>$value</code> parameter.</p>

<p>If the key does not exist the method returns 1.</p>

<p>No flags are currently defined for this method.</p>

</dd>
<dt id="$status-=-$X-&gt;put($key,-$value-[,-$flags])-;"><a class="permalink" href="DB_File.html#$status-=-$X-%3Eput($key,-$value-%5B,-$flags%5D)-;">#</a><a id="status-X-put-key-value-flags"></a><b>$status = $X-&gt;put($key, $value [, $flags]) ;</b></dt>
<dd>

<p>Stores the key/value pair in the database.</p>

<p>If you use either the R_IAFTER or R_IBEFORE flags, the <code>$key</code> parameter will have the record number of the inserted key/value pair set.</p>

<p>Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and R_SETCURSOR.</p>

</dd>
<dt id="$status-=-$X-&gt;del($key-[,-$flags])-;"><a class="permalink" href="DB_File.html#$status-=-$X-%3Edel($key-%5B,-$flags%5D)-;">#</a><a id="status-X-del-key-flags"></a><b>$status = $X-&gt;del($key [, $flags]) ;</b></dt>
<dd>

<p>Removes all key/value pairs with key <code>$key</code> from the database.</p>

<p>A return code of 1 means that the requested key was not in the database.</p>

<p>R_CURSOR is the only valid flag at present.</p>

</dd>
<dt id="$status-=-$X-&gt;fd-;"><a class="permalink" href="DB_File.html#$status-=-$X-%3Efd-;">#</a><a id="status-X-fd"></a><b>$status = $X-&gt;fd ;</b></dt>
<dd>

<p>Returns the file descriptor for the underlying database.</p>

<p>See <a href="DB_File.html#Locking%3A-The-Trouble-with-fd">&quot;Locking: The Trouble with fd&quot;</a> for an explanation for why you should not use <code>fd</code> to lock your database.</p>

</dd>
<dt id="$status-=-$X-&gt;seq($key,-$value,-$flags)-;"><a class="permalink" href="DB_File.html#$status-=-$X-%3Eseq($key,-$value,-$flags)-;">#</a><a id="status-X-seq-key-value-flags"></a><b>$status = $X-&gt;seq($key, $value, $flags) ;</b></dt>
<dd>

<p>This interface allows sequential retrieval from the database. See <a href="https://perldoc.perl.org/5.35.0/dbopen">dbopen</a> for full details.</p>

<p>Both the <code>$key</code> and <code>$value</code> parameters will be set to the key/value pair read from the database.</p>

<p>The flags parameter is mandatory. The valid flag values are R_CURSOR, R_FIRST, R_LAST, R_NEXT and R_PREV.</p>

</dd>
<dt id="$status-=-$X-&gt;sync([$flags])-;"><a class="permalink" href="DB_File.html#$status-=-$X-%3Esync(%5B$flags%5D)-;">#</a><a id="status-X-sync-flags"></a><b>$status = $X-&gt;sync([$flags]) ;</b></dt>
<dd>

<p>Flushes any cached buffers to disk.</p>

<p>R_RECNOSYNC is the only valid flag at present.</p>

</dd>
</dl>

<h1 id="DBM-FILTERS"><a class="permalink" href="DB_File.html#DBM-FILTERS">#</a><a id="DBM"></a>DBM FILTERS</h1>

<p>A DBM Filter is a piece of code that is be used when you <i>always</i> want to make the same transformation to all keys and/or values in a DBM database. An example is when you need to encode your data in UTF-8 before writing to the database and then decode the UTF-8 when reading from the database file.</p>

<p>There are two ways to use a DBM Filter.</p>

<ol>

<li><p>Using the low-level API defined below.</p>

</li>
<li><p>Using the <a href="DBM_Filter.html">DBM_Filter</a> module. This module hides the complexity of the API defined below and comes with a number of &quot;canned&quot; filters that cover some of the common use-cases.</p>

</li>
</ol>

<p>Use of the <a href="DBM_Filter.html">DBM_Filter</a> module is recommended.</p>

<h2 id="DBM-Filter-Low-level-API"><a class="permalink" href="DB_File.html#DBM-Filter-Low-level-API">#</a><a id="DBM1"></a>DBM Filter Low-level API</h2>

<p>There are four methods associated with DBM Filters. All work identically, and each is used to install (or uninstall) a single DBM Filter. Each expects a single parameter, namely a reference to a sub. The only difference between them is the place that the filter is installed.</p>

<p>To summarise:</p>

<dl>

<dt id="filter_store_key"><a class="permalink" href="DB_File.html#filter_store_key">#</a><b>filter_store_key</b></dt>
<dd>

<p>If a filter has been installed with this method, it will be invoked every time you write a key to a DBM database.</p>

</dd>
<dt id="filter_store_value"><a class="permalink" href="DB_File.html#filter_store_value">#</a><b>filter_store_value</b></dt>
<dd>

<p>If a filter has been installed with this method, it will be invoked every time you write a value to a DBM database.</p>

</dd>
<dt id="filter_fetch_key"><a class="permalink" href="DB_File.html#filter_fetch_key">#</a><b>filter_fetch_key</b></dt>
<dd>

<p>If a filter has been installed with this method, it will be invoked every time you read a key from a DBM database.</p>

</dd>
<dt id="filter_fetch_value"><a class="permalink" href="DB_File.html#filter_fetch_value">#</a><b>filter_fetch_value</b></dt>
<dd>

<p>If a filter has been installed with this method, it will be invoked every time you read a value from a DBM database.</p>

</dd>
</dl>

<p>You can use any combination of the methods, from none, to all four.</p>

<p>All filter methods return the existing filter, if present, or <code>undef</code> in not.</p>

<p>To delete a filter pass <code>undef</code> to it.</p>

<h2 id="The-Filter"><a class="permalink" href="DB_File.html#The-Filter">#</a><a id="The4"></a>The Filter</h2>

<p>When each filter is called by Perl, a local copy of <code>$_</code> will contain the key or value to be filtered. Filtering is achieved by modifying the contents of <code>$_</code>. The return code from the filter is ignored.</p>

<h2 id="An-Example-the-NULL-termination-problem."><a class="permalink" href="DB_File.html#An-Example-the-NULL-termination-problem.">#</a><a id="An"></a><a id="An-Example----the-NULL-termination-problem"></a>An Example -- the NULL termination problem.</h2>

<p>Consider the following scenario. You have a DBM database that you need to share with a third-party C application. The C application assumes that <i>all</i> keys and values are NULL terminated. Unfortunately when Perl writes to DBM databases it doesn&#39;t use NULL termination, so your Perl application will have to manage NULL termination itself. When you write to the database you will have to use something like this:</p>

<pre><code>$hash{&quot;$key\0&quot;} = &quot;$value\0&quot; ;</code></pre>

<p>Similarly the NULL needs to be taken into account when you are considering the length of existing keys/values.</p>

<p>It would be much better if you could ignore the NULL terminations issue in the main application code and have a mechanism that automatically added the terminating NULL to all keys and values whenever you write to the database and have them removed when you read from the database. As I&#39;m sure you have already guessed, this is a problem that DBM Filters can fix very easily.</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;

my %hash ;
my $filename = &quot;filt&quot; ;
unlink $filename ;

my $db = tie %hash, &#39;DB_File&#39;, $filename, O_CREAT|O_RDWR, 0666, $DB_HASH
  or die &quot;Cannot open $filename: $!\n&quot; ;

# Install DBM Filters
$db-&gt;filter_fetch_key  ( sub { s/\0$//    } ) ;
$db-&gt;filter_store_key  ( sub { $_ .= &quot;\0&quot; } ) ;
$db-&gt;filter_fetch_value( sub { s/\0$//    } ) ;
$db-&gt;filter_store_value( sub { $_ .= &quot;\0&quot; } ) ;

$hash{&quot;abc&quot;} = &quot;def&quot; ;
my $a = $hash{&quot;ABC&quot;} ;
# ...
undef $db ;
untie %hash ;</code></pre>

<p>Hopefully the contents of each of the filters should be self-explanatory. Both &quot;fetch&quot; filters remove the terminating NULL, and both &quot;store&quot; filters add a terminating NULL.</p>

<h2 id="Another-Example-Key-is-a-C-int."><a class="permalink" href="DB_File.html#Another-Example-Key-is-a-C-int.">#</a><a id="Another1"></a><a id="Another-Example----Key-is-a-C-int"></a>Another Example -- Key is a C int.</h2>

<p>Here is another real-life example. By default, whenever Perl writes to a DBM database it always writes the key and value as strings. So when you use this:</p>

<pre><code>$hash{12345} = &quot;something&quot; ;</code></pre>

<p>the key 12345 will get stored in the DBM database as the 5 byte string &quot;12345&quot;. If you actually want the key to be stored in the DBM database as a C int, you will have to use <code>pack</code> when writing, and <code>unpack</code> when reading.</p>

<p>Here is a DBM Filter that does it:</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;
my %hash ;
my $filename = &quot;filt&quot; ;
unlink $filename ;


my $db = tie %hash, &#39;DB_File&#39;, $filename, O_CREAT|O_RDWR, 0666, $DB_HASH
  or die &quot;Cannot open $filename: $!\n&quot; ;

$db-&gt;filter_fetch_key  ( sub { $_ = unpack(&quot;i&quot;, $_) } ) ;
$db-&gt;filter_store_key  ( sub { $_ = pack (&quot;i&quot;, $_) } ) ;
$hash{123} = &quot;def&quot; ;
# ...
undef $db ;
untie %hash ;</code></pre>

<p>This time only two filters have been used -- we only need to manipulate the contents of the key, so it wasn&#39;t necessary to install any value filters.</p>

<h1 id="HINTS-AND-TIPS"><a class="permalink" href="DB_File.html#HINTS-AND-TIPS">#</a><a id="HINTS"></a>HINTS AND TIPS</h1>

<h2 id="Locking:-The-Trouble-with-fd"><a class="permalink" href="DB_File.html#Locking:-The-Trouble-with-fd">#</a><a id="Locking"></a>Locking: The Trouble with fd</h2>

<p>Until version 1.72 of this module, the recommended technique for locking <b>DB_File</b> databases was to flock the filehandle returned from the &quot;fd&quot; function. Unfortunately this technique has been shown to be fundamentally flawed (Kudos to David Harris for tracking this down). Use it at your own peril!</p>

<p>The locking technique went like this.</p>

<pre><code>$db = tie(%db, &#39;DB_File&#39;, &#39;foo.db&#39;, O_CREAT|O_RDWR, 0644)
    || die &quot;dbcreat foo.db $!&quot;;
$fd = $db-&gt;fd;
open(DB_FH, &quot;+&lt;&amp;=$fd&quot;) || die &quot;dup $!&quot;;
flock (DB_FH, LOCK_EX) || die &quot;flock: $!&quot;;
...
$db{&quot;Tom&quot;} = &quot;Jerry&quot; ;
...
flock(DB_FH, LOCK_UN);
undef $db;
untie %db;
close(DB_FH);</code></pre>

<p>In simple terms, this is what happens:</p>

<ol>

<li><p>Use &quot;tie&quot; to open the database.</p>

</li>
<li><p>Lock the database with fd &amp; flock.</p>

</li>
<li><p>Read &amp; Write to the database.</p>

</li>
<li><p>Unlock and close the database.</p>

</li>
</ol>

<p>Here is the crux of the problem. A side-effect of opening the <b>DB_File</b> database in step 2 is that an initial block from the database will get read from disk and cached in memory.</p>

<p>To see why this is a problem, consider what can happen when two processes, say &quot;A&quot; and &quot;B&quot;, both want to update the same <b>DB_File</b> database using the locking steps outlined above. Assume process &quot;A&quot; has already opened the database and has a write lock, but it hasn&#39;t actually updated the database yet (it has finished step 2, but not started step 3 yet). Now process &quot;B&quot; tries to open the same database - step 1 will succeed, but it will block on step 2 until process &quot;A&quot; releases the lock. The important thing to notice here is that at this point in time both processes will have cached identical initial blocks from the database.</p>

<p>Now process &quot;A&quot; updates the database and happens to change some of the data held in the initial buffer. Process &quot;A&quot; terminates, flushing all cached data to disk and releasing the database lock. At this point the database on disk will correctly reflect the changes made by process &quot;A&quot;.</p>

<p>With the lock released, process &quot;B&quot; can now continue. It also updates the database and unfortunately it too modifies the data that was in its initial buffer. Once that data gets flushed to disk it will overwrite some/all of the changes process &quot;A&quot; made to the database.</p>

<p>The result of this scenario is at best a database that doesn&#39;t contain what you expect. At worst the database will corrupt.</p>

<p>The above won&#39;t happen every time competing process update the same <b>DB_File</b> database, but it does illustrate why the technique should not be used.</p>

<h2 id="Safe-ways-to-lock-a-database"><a class="permalink" href="DB_File.html#Safe-ways-to-lock-a-database">#</a><a id="Safe"></a>Safe ways to lock a database</h2>

<p>Starting with version 2.x, Berkeley DB has internal support for locking. The companion module to this one, <a href="https://metacpan.org/pod/BerkeleyDB">BerkeleyDB</a>, provides an interface to this locking functionality. If you are serious about locking Berkeley DB databases, I strongly recommend using <a href="https://metacpan.org/pod/BerkeleyDB">BerkeleyDB</a>.</p>

<p>If using <a href="https://metacpan.org/pod/BerkeleyDB">BerkeleyDB</a> isn&#39;t an option, there are a number of modules available on CPAN that can be used to implement locking. Each one implements locking differently and has different goals in mind. It is therefore worth knowing the difference, so that you can pick the right one for your application. Here are the three locking wrappers:</p>

<dl>

<dt id="Tie::DB_Lock"><a class="permalink" href="DB_File.html#Tie::DB_Lock">#</a><a id="Tie"></a><b>Tie::DB_Lock</b></dt>
<dd>

<p>A <b>DB_File</b> wrapper which creates copies of the database file for read access, so that you have a kind of a multiversioning concurrent read system. However, updates are still serial. Use for databases where reads may be lengthy and consistency problems may occur.</p>

</dd>
<dt id="Tie::DB_LockFile"><a class="permalink" href="DB_File.html#Tie::DB_LockFile">#</a><a id="Tie1"></a><b>Tie::DB_LockFile</b></dt>
<dd>

<p>A <b>DB_File</b> wrapper that has the ability to lock and unlock the database while it is being used. Avoids the tie-before-flock problem by simply re-tie-ing the database when you get or drop a lock. Because of the flexibility in dropping and re-acquiring the lock in the middle of a session, this can be massaged into a system that will work with long updates and/or reads if the application follows the hints in the POD documentation.</p>

</dd>
<dt id="DB_File::Lock"><a class="permalink" href="DB_File.html#DB_File::Lock">#</a><a id="DB_File"></a><b>DB_File::Lock</b></dt>
<dd>

<p>An extremely lightweight <b>DB_File</b> wrapper that simply flocks a lockfile before tie-ing the database and drops the lock after the untie. Allows one to use the same lockfile for multiple databases to avoid deadlock problems, if desired. Use for databases where updates are reads are quick and simple flock locking semantics are enough.</p>

</dd>
</dl>

<h2 id="Sharing-Databases-With-C-Applications"><a class="permalink" href="DB_File.html#Sharing-Databases-With-C-Applications">#</a><a id="Sharing"></a>Sharing Databases With C Applications</h2>

<p>There is no technical reason why a Berkeley DB database cannot be shared by both a Perl and a C application.</p>

<p>The vast majority of problems that are reported in this area boil down to the fact that C strings are NULL terminated, whilst Perl strings are not. See <a href="DB_File.html#DBM-FILTERS">&quot;DBM FILTERS&quot;</a> for a generic way to work around this problem.</p>

<p>Here is a real example. Netscape 2.0 keeps a record of the locations you visit along with the time you last visited them in a DB_HASH database. This is usually stored in the file <i>~/.netscape/history.db</i>. The key field in the database is the location string and the value field is the time the location was last visited stored as a 4 byte binary value.</p>

<p>If you haven&#39;t already guessed, the location string is stored with a terminating NULL. This means you need to be careful when accessing the database.</p>

<p>Here is a snippet of code that is loosely based on Tom Christiansen&#39;s <i>ggh</i> script (available from your nearest CPAN archive in <i>authors/id/TOMC/scripts/nshist.gz</i>).</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;
use Fcntl ;

my ($dotdir, $HISTORY, %hist_db, $href, $binary_time, $date) ;
$dotdir = $ENV{HOME} || $ENV{LOGNAME};

$HISTORY = &quot;$dotdir/.netscape/history.db&quot;;

tie %hist_db, &#39;DB_File&#39;, $HISTORY
    or die &quot;Cannot open $HISTORY: $!\n&quot; ;;

# Dump the complete database
while ( ($href, $binary_time) = each %hist_db ) {

    # remove the terminating NULL
    $href =~ s/\x00$// ;

    # convert the binary time into a user friendly string
    $date = localtime unpack(&quot;V&quot;, $binary_time);
    print &quot;$date $href\n&quot; ;
}

# check for the existence of a specific key
# remember to add the NULL
if ( $binary_time = $hist_db{&quot;http://mox.perl.com/\x00&quot;} ) {
    $date = localtime unpack(&quot;V&quot;, $binary_time) ;
    print &quot;Last visited mox.perl.com on $date\n&quot; ;
}
else {
    print &quot;Never visited mox.perl.com\n&quot;
}

untie %hist_db ;</code></pre>

<h2 id="The-untie()-Gotcha"><a class="permalink" href="DB_File.html#The-untie()-Gotcha">#</a><a id="The5"></a><a id="The-untie-Gotcha"></a>The untie() Gotcha</h2>

<p>If you make use of the Berkeley DB API, it is <i>very</i> strongly recommended that you read <a href="perltie.html#The-untie-Gotcha">&quot;The untie Gotcha&quot; in perltie</a>.</p>

<p>Even if you don&#39;t currently make use of the API interface, it is still worth reading it.</p>

<p>Here is an example which illustrates the problem from a <b>DB_File</b> perspective:</p>

<pre><code>use DB_File ;
use Fcntl ;

my %x ;
my $X ;

$X = tie %x, &#39;DB_File&#39;, &#39;tst.fil&#39; , O_RDWR|O_TRUNC
    or die &quot;Cannot tie first time: $!&quot; ;

$x{123} = 456 ;

untie %x ;

tie %x, &#39;DB_File&#39;, &#39;tst.fil&#39; , O_RDWR|O_CREAT
    or die &quot;Cannot tie second time: $!&quot; ;

untie %x ;</code></pre>

<p>When run, the script will produce this error message:</p>

<pre><code class="plaintext">Cannot tie second time: Invalid argument at bad.file line 14.</code></pre>

<p>Although the error message above refers to the second tie() statement in the script, the source of the problem is really with the untie() statement that precedes it.</p>

<p>Having read <a href="perltie.html">perltie</a> you will probably have already guessed that the error is caused by the extra copy of the tied object stored in <code>$X</code>. If you haven&#39;t, then the problem boils down to the fact that the <b>DB_File</b> destructor, DESTROY, will not be called until <i>all</i> references to the tied object are destroyed. Both the tied variable, <code>%x</code>, and <code>$X</code> above hold a reference to the object. The call to untie() will destroy the first, but <code>$X</code> still holds a valid reference, so the destructor will not get called and the database file <i>tst.fil</i> will remain open. The fact that Berkeley DB then reports the attempt to open a database that is already open via the catch-all &quot;Invalid argument&quot; doesn&#39;t help.</p>

<p>If you run the script with the <code>-w</code> flag the error message becomes:</p>

<pre><code class="plaintext">untie attempted while 1 inner references still exist at bad.file line 12.
Cannot tie second time: Invalid argument at bad.file line 14.</code></pre>

<p>which pinpoints the real problem. Finally the script can now be modified to fix the original problem by destroying the API object before the untie:</p>

<pre><code>...
$x{123} = 456 ;

undef $X ;
untie %x ;

$X = tie %x, &#39;DB_File&#39;, &#39;tst.fil&#39; , O_RDWR|O_CREAT
...</code></pre>

<h1 id="COMMON-QUESTIONS"><a class="permalink" href="DB_File.html#COMMON-QUESTIONS">#</a><a id="COMMON"></a>COMMON QUESTIONS</h1>

<h2 id="Why-is-there-Perl-source-in-my-database?"><a class="permalink" href="DB_File.html#Why-is-there-Perl-source-in-my-database?">#</a><a id="Why"></a><a id="Why-is-there-Perl-source-in-my-database"></a>Why is there Perl source in my database?</h2>

<p>If you look at the contents of a database file created by DB_File, there can sometimes be part of a Perl script included in it.</p>

<p>This happens because Berkeley DB uses dynamic memory to allocate buffers which will subsequently be written to the database file. Being dynamic, the memory could have been used for anything before DB malloced it. As Berkeley DB doesn&#39;t clear the memory once it has been allocated, the unused portions will contain random junk. In the case where a Perl script gets written to the database, the random junk will correspond to an area of dynamic memory that happened to be used during the compilation of the script.</p>

<p>Unless you don&#39;t like the possibility of there being part of your Perl scripts embedded in a database file, this is nothing to worry about.</p>

<h2 id="How-do-I-store-complex-data-structures-with-DB_File?"><a class="permalink" href="DB_File.html#How-do-I-store-complex-data-structures-with-DB_File?">#</a><a id="How"></a><a id="How-do-I-store-complex-data-structures-with-DB_File"></a>How do I store complex data structures with DB_File?</h2>

<p>Although <b>DB_File</b> cannot do this directly, there is a module which can layer transparently over <b>DB_File</b> to accomplish this feat.</p>

<p>Check out the MLDBM module, available on CPAN in the directory <i>modules/by-module/MLDBM</i>.</p>

<h2 id="What-does-&quot;wide-character-in-subroutine-entry&quot;-mean?"><a class="permalink" href="DB_File.html#What-does-%22wide-character-in-subroutine-entry%22-mean?">#</a><a id="What"></a><a id="What-does-wide-character-in-subroutine-entry-mean"></a>What does &quot;wide character in subroutine entry&quot; mean?</h2>

<p>You will usually get this message if you are working with UTF-8 data and want to read/write it from/to a Berkeley DB database file.</p>

<p>The easist way to deal with this issue is to use the pre-defined &quot;utf8&quot; <b>DBM_Filter</b> (see <a href="DBM_Filter.html">DBM_Filter</a>) that was designed to deal with this situation.</p>

<p>The example below shows what you need if <i>both</i> the key and value are expected to be in UTF-8.</p>

<pre><code>use DB_File;
use DBM_Filter;

my $db = tie %h, &#39;DB_File&#39;, &#39;/tmp/try.db&#39;, O_CREAT|O_RDWR, 0666, $DB_BTREE;
$db-&gt;Filter_Key_Push(&#39;utf8&#39;);
$db-&gt;Filter_Value_Push(&#39;utf8&#39;);

my $key = &quot;\N{LATIN SMALL LETTER A WITH ACUTE}&quot;;
my $value = &quot;\N{LATIN SMALL LETTER E WITH ACUTE}&quot;;
$h{ $key } = $value;</code></pre>

<h2 id="What-does-&quot;Invalid-Argument&quot;-mean?"><a class="permalink" href="DB_File.html#What-does-%22Invalid-Argument%22-mean?">#</a><a id="What1"></a><a id="What-does-Invalid-Argument-mean"></a>What does &quot;Invalid Argument&quot; mean?</h2>

<p>You will get this error message when one of the parameters in the <code>tie</code> call is wrong. Unfortunately there are quite a few parameters to get wrong, so it can be difficult to figure out which one it is.</p>

<p>Here are a couple of possibilities:</p>

<ol>

<li><p>Attempting to reopen a database without closing it.</p>

</li>
<li><p>Using the O_WRONLY flag.</p>

</li>
</ol>

<h2 id="What-does-&quot;Bareword-&#39;DB_File&#39;-not-allowed&quot;-mean?"><a class="permalink" href="DB_File.html#What-does-%22Bareword-&#39;DB_File&#39;-not-allowed%22-mean?">#</a><a id="What2"></a><a id="What-does-Bareword-DB_File-not-allowed-mean"></a>What does &quot;Bareword &#39;DB_File&#39; not allowed&quot; mean?</h2>

<p>You will encounter this particular error message when you have the <code>strict &#39;subs&#39;</code> pragma (or the full strict pragma) in your script. Consider this script:</p>

<pre><code>use warnings ;
use strict ;
use DB_File ;
my %x ;
tie %x, DB_File, &quot;filename&quot; ;</code></pre>

<p>Running it produces the error in question:</p>

<pre><code class="plaintext">Bareword &quot;DB_File&quot; not allowed while &quot;strict subs&quot; in use</code></pre>

<p>To get around the error, place the word <code>DB_File</code> in either single or double quotes, like this:</p>

<pre><code>tie %x, &quot;DB_File&quot;, &quot;filename&quot; ;</code></pre>

<p>Although it might seem like a real pain, it is really worth the effort of having a <code>use strict</code> in all your scripts.</p>

<h1 id="REFERENCES"><a class="permalink" href="DB_File.html#REFERENCES">#</a>REFERENCES</h1>

<p>Articles that are either about <b>DB_File</b> or make use of it.</p>

<ol>

<li><p><i>Full-Text Searching in Perl</i>, Tim Kientzle (tkientzle@ddj.com), Dr. Dobb&#39;s Journal, Issue 295, January 1999, pp 34-41</p>

</li>
</ol>

<h1 id="HISTORY"><a class="permalink" href="DB_File.html#HISTORY">#</a>HISTORY</h1>

<p>Moved to the Changes file.</p>

<h1 id="BUGS"><a class="permalink" href="DB_File.html#BUGS">#</a>BUGS</h1>

<p>Some older versions of Berkeley DB had problems with fixed length records using the RECNO file format. This problem has been fixed since version 1.85 of Berkeley DB.</p>

<p>I am sure there are bugs in the code. If you do find any, or can suggest any enhancements, I would welcome your comments.</p>

<h1 id="SUPPORT"><a class="permalink" href="DB_File.html#SUPPORT">#</a>SUPPORT</h1>

<p>General feedback/questions/bug reports should be sent to <a href="https://github.com/pmqs/DB_File/issues">https://github.com/pmqs/DB_File/issues</a> (preferred) or <a href="https://rt.cpan.org/Public/Dist/Display.html?Name=DB_File">https://rt.cpan.org/Public/Dist/Display.html?Name=DB_File</a>.</p>

<h1 id="AVAILABILITY"><a class="permalink" href="DB_File.html#AVAILABILITY">#</a>AVAILABILITY</h1>

<p><b>DB_File</b> comes with the standard Perl source distribution. Look in the directory <i>ext/DB_File</i>. Given the amount of time between releases of Perl the version that ships with Perl is quite likely to be out of date, so the most recent version can always be found on CPAN (see <a href="perlmodlib.html#CPAN">&quot;CPAN&quot; in perlmodlib</a> for details), in the directory <i>modules/by-module/DB_File</i>.</p>

<p><b>DB_File</b> is designed to work with any version of Berkeley DB, but is limited to the functionality provided by version 1. If you want to make use of the new features available in Berkeley DB 2.x, or greater, use the Perl module <a href="https://metacpan.org/pod/BerkeleyDB">BerkeleyDB</a> instead.</p>

<p>The official web site for Berkeley DB is <a href="http://www.oracle.com/technology/products/berkeley-db/db/index.html">http://www.oracle.com/technology/products/berkeley-db/db/index.html</a>. All versions of Berkeley DB are available there.</p>

<p>Alternatively, Berkeley DB version 1 is available at your nearest CPAN archive in <i>src/misc/db.1.85.tar.gz</i>.</p>

<h1 id="COPYRIGHT"><a class="permalink" href="DB_File.html#COPYRIGHT">#</a>COPYRIGHT</h1>

<p>Copyright (c) 1995-2020 Paul Marquess. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p>

<p>Although <b>DB_File</b> is covered by the Perl license, the library it makes use of, namely Berkeley DB, is not. Berkeley DB has its own copyright and its own license. Please take the time to read it.</p>

<p>Here are a few words taken from the Berkeley DB FAQ (at <a href="http://www.oracle.com/technology/products/berkeley-db/db/index.html">http://www.oracle.com/technology/products/berkeley-db/db/index.html</a>) regarding the license:</p>

<pre><code class="plaintext">Do I have to license DB to use it in Perl scripts?

No. The Berkeley DB license requires that software that uses
Berkeley DB be freely redistributable. In the case of Perl, that
software is Perl, and not your scripts. Any Perl scripts that you
write are your property, including scripts that make use of
Berkeley DB. Neither the Perl license nor the Berkeley DB license
place any restriction on what you may do with them.</code></pre>

<p>If you are in any doubt about the license situation, contact either the Berkeley DB authors or the author of DB_File. See <a href="DB_File.html#AUTHOR">&quot;AUTHOR&quot;</a> for details.</p>

<h1 id="SEE-ALSO"><a class="permalink" href="DB_File.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p><a href="perl.html">perl</a>, <a href="http://man.he.net/man3/dbopen">dbopen(3)</a>, <a href="http://man.he.net/man3/hash">hash(3)</a>, <a href="http://man.he.net/man3/recno">recno(3)</a>, <a href="http://man.he.net/man3/btree">btree(3)</a>, <a href="perldbmfilter.html">perldbmfilter</a>, <a href="DBM_Filter.html">DBM_Filter</a></p>

<h1 id="AUTHOR"><a class="permalink" href="DB_File.html#AUTHOR">#</a>AUTHOR</h1>

<p>The DB_File interface was written by Paul Marquess &lt;pmqs@cpan.org&gt;.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
